﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using System.Text;

namespace Runda.CMS.Portal.Report
{
    public partial class SalesRpt_I3 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 2012; i <= 2015; i++)
                {
                    ddlBudgetYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                

                string sqlWhere = getWhereClause();
                LoadContractTrack(sqlWhere);

            }
        }

        private void setdrData(string strHead1,string strHead2, ref DataTable dt,string sqlOldWhere, string strsqlWhere,BillBll bll)
        {
            DataRow dr;
            DataRow drtmp;
            DataTable dttmp = bll.GetSaleRevenue(sqlOldWhere + strsqlWhere);

            dr = dt.NewRow();
            drtmp = dttmp.Rows[0];

            dr["col1"] = strHead1;
            dr["col2"] = strHead2;
            dr["col3"] = drtmp["Amount"];
            dr["col4"] = drtmp["Cost"];

            dt.Rows.Add(dr);

        }
        private void LoadContractTrack(string sqlWhere)
        {
            //set reference
            ReportViewer ReportViewer = rvReporting;

            //get rdlc path
            string strRootPath = this.Server.MapPath(this.Request.ApplicationPath);
            string strFilePath ;

            strFilePath = "report/rdlc/SalesRpt_I3.rdlc";

            //set report viewer property
            ReportViewer.SizeToReportContent = true;
            ReportViewer.ZoomMode = ZoomMode.Percent;
            ReportViewer.AsyncRendering = false;
            
            //set report viewer work mode
            ReportViewer.ProcessingMode = ProcessingMode.Local;
            
            LocalReport lp = ReportViewer.LocalReport;
            lp.ReportPath = strFilePath;
            
            //get dataset  
            BillBll bll = new BillBll();
            //DataSet ds = new DataSet();
            //ds = bll.SalesRpt_E1(sqlWhere, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue);

            //ds.DataSetName = "SalesDataSet_rpt_I1";
            //set data set
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "SalesDataSet_rpt_I1";

            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("col1", typeof(string)));
            dt.Columns.Add(new DataColumn("col2", typeof(string)));
            dt.Columns.Add(new DataColumn("col3", typeof(decimal)));
            dt.Columns.Add(new DataColumn("col4", typeof(decimal)));

            setdrData("生化", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('010101','020101','010201','010301','011401','011801','019901','020201','020901','010601')", bll);
            setdrData("|_干生化", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('010101','020101')", bll);
            setdrData("|_湿生化", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('010201','010301','011401','011801','019901','020201','020901','010601')", bll);
            setdrData("|___其中：代理", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('010201','010301','011401','011801','019901','020201','020901')", bll);
            setdrData("|_________自产", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('010601')", bll);

            setdrData("免疫", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('010102','010202','010402','011802','019904','020102','020202','020301','020104','020204')", bll);
            setdrData("血型", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('010103','020103')", bll);
            setdrData("血球", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('010203','019910','020203','020601','011202')", bll);
            setdrData("微生物", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('011601','010704','019907','020402','021201')", bll);
            setdrData("尿液", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('0115','011604','011803','011204','019903','0211','021204','020603')", bll);
            setdrData("血凝", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('010501','011201','011605','019905','020501','020602')", bll);

            setdrData("糖化", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('011001','011101','011203','011602','019908','020701','020801','021202','010902','021002','020302')", bll);
            setdrData("|_其中：代理", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('011001','011101','011203','011602','019908','020701','020801','021202','020302')", bll);
            setdrData("|_______自产", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('010902','021002')", bll);

            setdrData("血气", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('011603','019902','021203','010901','021001','010602','010204','020205')", bll);
            setdrData("|_其中：代理", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('011603','019902','021203','010204','020205')", bll);
            setdrData("|_______自产", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('010901','021001','010602')", bll);

            setdrData("第三方质控品", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('010401')", bll);
            setdrData("采血", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('010701','019906','010303')", bll);
            setdrData("病理", "", ref dt, sqlWhere, " and left(CategoryCode,6) in ('010703','020403')", bll);
            setdrData("其他仪器和试剂", "", ref dt, sqlWhere, " and left(CategoryCode,2) in ('01','02') and  left(CategoryCode,6) not in ('010101','020101','010201','010301','011401','011801','019901','020201','020901','010601','010102','010202','010402','011802','019904','020102','020202','020301','020104','010103','020103','010203','010602','019910','020203','020601','011601','010704','019907','020402','021201','0115','011604','011803','011204','019903','0211','021204','020603','010501','011201','011605','019905','020501','020602','011001','011101','011203','011602','019908','020603','020701','020801','021202','010902','021002','011603','019902','021203','010901','021001','010401','010701','019906','010303','010703','020403','020204','011202','020302','010204','020205')", bll);
            setdrData("零配件", "", ref dt, sqlWhere, " and left(CategoryCode,2) in ('03','04','99')", bll);


            setdrData("维修保养", "", ref dt, sqlWhere, " and CategoryCode like '07%'", bll);
            setdrData("合计", "", ref dt, sqlWhere, "", bll);
            
            rds.Value = dt;
            lp.DataSources.Clear();
            lp.DataSources.Add(rds);

            ReportParameter rp_Customername = new ReportParameter("rp_TotalAmount", dt.Rows[dt.Rows.Count - 1]["col3"].ToString());
            ReportViewer.LocalReport.SetParameters(new ReportParameter[] { rp_Customername });
            ReportViewer.LocalReport.Refresh(); ;
            
        }

        private string getWhereClause()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" and billYear = " + ddlBudgetYear.SelectedValue.ToString() );
            sb.Append(" and billMonthex >= " + ddlBudgetMonth.SelectedValue.ToString());
            sb.Append(" and billMonthex <= " + ddlBudgetMonth1.SelectedValue.ToString());

            return sb.ToString();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string sqlWhere = getWhereClause();
            LoadContractTrack(sqlWhere);
        }

        
    }
}
